package com.jeesite.modules.exam.quota.web;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.exam.notice.entity.DcSupExamNotice;
import com.jeesite.modules.exam.quota.dto.ExamQuotaDataDto;
import com.jeesite.modules.exam.quota.dto.ExamQuotaDataExport;
import com.jeesite.modules.exam.quota.dto.GroupByTypeCount;
import com.jeesite.modules.exam.quota.dto.VersionInfoDto;
import com.jeesite.modules.exam.quota.entity.*;
import com.jeesite.modules.exam.quota.service.DcSupExamQuotaOrgService;
import com.jeesite.modules.exam.quota.service.DcSupExamSumQuotaInfoService;
import com.jeesite.modules.exam.score.entity.DcSupExamScore;
import com.jeesite.modules.exam.score.service.DcSupExamScoreService;
import com.jeesite.modules.exam.score.vo.DcSupExamQuotaOrgVo;
import com.jeesite.modules.exam.score.vo.DcSupExamVo;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.utils.OfficeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

import java.util.stream.Collectors;

/**
 * 考核任务指标汇总表Controller
 * @author xhc
 * @version 2022-06-09
 */
@Controller
@RequestMapping(value = "${adminPath}/quota/dcSupExamSumQuotaInfo")
public class DcSupExamSumQuotaInfoController extends BaseController {

	@Autowired
	private DcSupExamSumQuotaInfoService dcSupExamSumQuotaInfoService;
	@Autowired
	private DcSupExamQuotaOrgService dcSupExamQuotaOrgService;
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupExamSumQuotaInfo get(String id, boolean isNewRecord) {
		return dcSupExamSumQuotaInfoService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, Model model) {
		model.addAttribute("dcSupExamSumQuotaInfo", dcSupExamSumQuotaInfo);
		List<VersionInfoDto> version = dcSupExamSumQuotaInfoService.queryVersion(dcSupExamSumQuotaInfo);
		Integer isPush= dcSupExamSumQuotaInfoService.isPush(dcSupExamSumQuotaInfo.getExamId(),"2");
		if (!CollectionUtils.isEmpty(version)){
			VersionInfoDto data=new VersionInfoDto();
			data.setVersion(" ");
			data.setVersionName("初 始 版 本");
			version.add(version.size(), data);
		}
		model.addAttribute("isPush",isPush==null?999:isPush);
		model.addAttribute("versions",version);
		if (StringUtils.isEmpty(dcSupExamSumQuotaInfo.getRemarks3()) || "0".equals(dcSupExamSumQuotaInfo.getRemarks3())){
			return "modules/exam/quota/dcSupExamSumQuotaInfoList";
		}else{
			return "modules/exam/quota/dcSupExamSumQuotaInfoListDemo";
		}
		//model.addAttribute("batchFlag", StringUtils.isEmpty(dcSupExamSumQuotaInfo.getRemarks3())?"0":dcSupExamSumQuotaInfo.getRemarks3());

	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ExamQuotaDataDto> listData(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo,Model model ,HttpServletRequest request, HttpServletResponse response) {
		dcSupExamSumQuotaInfo.setPage(new Page<>(request, response));
		Page<ExamQuotaDataDto> page = dcSupExamSumQuotaInfoService.queryListData(dcSupExamSumQuotaInfo);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("quota:dcSupExamSumQuotaInfo:view")
	@RequestMapping(value = "form")
	public String form(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, Model model) {
		model.addAttribute("dcSupExamSumQuotaInfo", dcSupExamSumQuotaInfo);
		return "modules/exam/quota/dcSupExamSumQuotaInfoForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("quota:dcSupExamSumQuotaInfo:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		dcSupExamSumQuotaInfoService.save(dcSupExamSumQuotaInfo);
		return renderResult(Global.TRUE, text("保存考核任务指标汇总表成功！"));
	}

	/**
	 * 保存数据
	 */
	@PostMapping(value = "saveStrList")
	@ResponseBody
	public String saveStrList( String dcSupExamSumQuotaInfoList) {
		List<DcSupExamSumQuotaInfo> dcSupExamSumQuotaInfos = JSONObject.parseArray(dcSupExamSumQuotaInfoList, DcSupExamSumQuotaInfo.class);
		dcSupExamSumQuotaInfoService.saveStrList(dcSupExamSumQuotaInfos);
		return renderResult(Global.TRUE, text("保存考核任务指标成功！"));
	}


	/**
	 * 保存细则
	 */
	@PostMapping(value = "saveStrFile")
	@ResponseBody
	public String saveStrFile( String dcSupExamSumQuotaInfoList,String noticeId) {
		List<DcSupExamDetailFile> dcSupExamSumQuotaInfos = JSONObject.parseArray(dcSupExamSumQuotaInfoList, DcSupExamDetailFile.class);
		dcSupExamSumQuotaInfoService.saveStrFile(dcSupExamSumQuotaInfos,noticeId);
		return renderResult(Global.TRUE, text("细则补充成功！"));
	}



	/**
	 * 保存数据
	 */
	@PostMapping(value = "saveStrSumList")
	@ResponseBody
	public String saveStrSumList( String dcSupExamSumQuotaInfoList) {
		List<DcSupExamSumQuotaInfo> dcSupExamSumQuotaInfos = JSONObject.parseArray(dcSupExamSumQuotaInfoList, DcSupExamSumQuotaInfo.class);
		dcSupExamSumQuotaInfoService.saveStrSumList(dcSupExamSumQuotaInfos);
		return renderResult(Global.TRUE, text("保存考核任务指标成功！"));
	}

	/**
	 * 停用数据
	 */
	@RequiresPermissions("quota:dcSupExamSumQuotaInfo:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		dcSupExamSumQuotaInfo.setStatus(DcSupExamSumQuotaInfo.STATUS_DISABLE);
		dcSupExamSumQuotaInfoService.updateStatus(dcSupExamSumQuotaInfo);
		return renderResult(Global.TRUE, text("停用考核任务指标成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("quota:dcSupExamSumQuotaInfo:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		dcSupExamSumQuotaInfo.setStatus(DcSupExamSumQuotaInfo.STATUS_NORMAL);
		dcSupExamSumQuotaInfoService.updateStatus(dcSupExamSumQuotaInfo);
		return renderResult(Global.TRUE, text("启用考核任务指标成功"));
	}
	
	/**
	 * 删除数据
	 */
//	@RequiresPermissions("quota:dcSupExamSumQuotaInfo:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		dcSupExamSumQuotaInfoService.delete(dcSupExamSumQuotaInfo);
		return renderResult(Global.TRUE, text("删除考核任务指标成功！"));
	}




	/**
	 * 查询单项指标列表数据
	 */
	@RequestMapping(value = "listDanData")
	@ResponseBody
	public Page<DcSupExamSumQuotaInfo> listDanData(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamSumQuotaInfo.setPage(new Page<>(request, response));
		Page<DcSupExamSumQuotaInfo> page = dcSupExamSumQuotaInfoService.findDanPage(dcSupExamSumQuotaInfo);
		return page;
	}


	/**
	 * 查询单项指标列表数据
	 */
	@RequestMapping(value = "listSingleData")
	@ResponseBody
	public Page<DcSupExamSumQuotaInfo> listSingleData(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamSumQuotaInfo.setPage(new Page<>(request, response));
		Page<DcSupExamSumQuotaInfo> page = dcSupExamSumQuotaInfoService.findSinglePage(dcSupExamSumQuotaInfo);
		return page;
	}


	/**
	 * 查询单项指标组织列表数据
	 */
	@RequestMapping(value = "listSingleOrgData")
	@ResponseBody
	public Page<DcSupExamSumQuotaInfo> listSingleOrgData(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamSumQuotaInfo.setPage(new Page<>(request, response));
		Page<DcSupExamSumQuotaInfo> page = dcSupExamSumQuotaInfoService.findSingleOrgPage(dcSupExamSumQuotaInfo);
		return page;
	}


	/**
	 * 查询单项指标列表数据
	 */
	@RequestMapping(value = "listDetailFileData")
	@ResponseBody
	public Page<DcSupExamDetailFile> listDetailFileData(DcSupExamDetailFile dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamSumQuotaInfo.setPage(new Page<>(request, response));
		Page<DcSupExamDetailFile> page = dcSupExamSumQuotaInfoService.listDetailFileData(dcSupExamSumQuotaInfo);
		return page;
	}

	/**
	 * 查询关键指标版本信息
	 */
	@RequestMapping(value = "listKeyData")
	@ResponseBody
	public Page<DcSupExamSumQuotaInfo> listKeyData(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamSumQuotaInfo.setPage(new Page<>(request, response));
		Page<DcSupExamSumQuotaInfo> page = dcSupExamSumQuotaInfoService.findKeyPage(dcSupExamSumQuotaInfo);
		return page;
	}


	/**
	 * 查询关键指标版本信息
	 */
	@RequestMapping(value = "makeCopy")
	@ResponseBody
	public Object makeCopy(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		dcSupExamSumQuotaInfoService.makeCopy(dcSupExamSumQuotaInfo);
		return renderResult(Global.TRUE, text("生成副本成功！"));
	}


	/**
	 * 保存版本页面
	 */
	@RequestMapping(value = "saveVersionPage")
	public String saveVersionPage(DcSupExamSumQuota dcSupExamSumQuota,Model model) {
		model.addAttribute("dcSupExamSumQuota", dcSupExamSumQuota);
		return "modules/exam/quota/dcSupExamSumQuotaVersion";
	}

	/**
	 * 保存版本
	 */
	@RequestMapping(value = "saveVersion")
	@ResponseBody
	public Object saveVersion(DcSupExamSumQuota dcSupExamSumQuota) {
		String saveVersion = dcSupExamSumQuotaInfoService.saveVersion(dcSupExamSumQuota);
		if ("99999999".equals(saveVersion)){
			return renderResult(Global.FALSE, text("版本名称已存在！"));
		}
		return renderResult(Global.TRUE, text(saveVersion));
	}

	/**
	 * 保存版本
	 */
	@RequestMapping(value = "publish")
	@ResponseBody
	public Object publish(DcSupExamSumQuota dcSupExamSumQuota) {
		String publish = dcSupExamSumQuotaInfoService.publish(dcSupExamSumQuota);
		if (!publish.equals("9")){
			return renderResult(Global.FALSE, text(publish));
		}
		return renderResult(Global.TRUE, text("发布版本成功！"));
	}


	@RequestMapping(value = "batchAddQuota")
	public String batchAddQuota(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo,Model model){
		if (!StringUtils.isEmpty(dcSupExamSumQuotaInfo.getExamYear())){
			dcSupExamSumQuotaInfo.setExamYear(dcSupExamSumQuotaInfo.getExamYear().split(",")[0]);
		}
		model.addAttribute("dcSupExamSumQuotaInfo",dcSupExamSumQuotaInfo);
		return  "modules/exam/quota/dcSupExamSumQuotaInfoAddForm";
	}

	/**
	 * 保存数据
	 */
	@PostMapping(value = "saveQuotas")
	@ResponseBody
	public String saveQuotas(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		String o = dcSupExamSumQuotaInfoService.saveQuotas(dcSupExamSumQuotaInfo);
		if (o != null){
			return renderResult(Global.FALSE, text(o));
		}
		return renderResult(Global.TRUE, text("保存考核任务指标成功！"));
	}



	/**
	 * 保存版本
	 */
	@RequestMapping(value = "export")
	@ResponseBody
	public void export(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo,HttpServletRequest request, HttpServletResponse response){
		dcSupExamSumQuotaInfoService.export(dcSupExamSumQuotaInfo,request,response);
	}

	/**
	 * 导入指标汇总数据
	 */
	@ResponseBody
	@PostMapping(value = "importData")
	public String importData(MultipartFile file, String updateSupport,DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		try {
//			boolean isUpdateSupport = Global.YES.equals(updateSupport);
			String message = dcSupExamSumQuotaInfoService.importData(file, dcSupExamSumQuotaInfo);
			return renderResult(Global.TRUE, message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, ex.getMessage());
		}
	}

	/**
	 * 导出模板
	 * @return
	 */
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletResponse response) {
		String fileName = "指标汇总导入模板.xlsx";
		try(ExcelExport ee = new ExcelExport("  ", ExamQuotaDataDto.class, ExcelField.Type.IMPORT)){
			ee.write(response, fileName);
		}
	}

	@RequestMapping("quotaPlay")
	public String quotaPlay(Model model){
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		return "modules/exam/scoresResult/dcSupExamQuotaPlayIndex";
	}

	@RequestMapping("quotaPlay2")
	public String quotaPlay2(Model model){
		return "modules/exam/scoresResult/dcSupExamQuotaPlayIndex2";
	}

	@RequestMapping("quotaResults")
	@ResponseBody
	public Page<ExamQuotaDataDto> quotaResults(ExamQuotaDataDto examQuotaDataDto, HttpServletRequest request, HttpServletResponse response) {
		examQuotaDataDto.setPage(new Page<>(request, response));
		return dcSupExamSumQuotaInfoService.quotaResults(examQuotaDataDto);
	}
	@RequestMapping("quotaResultsAll")
	@ResponseBody
	public Page<ExamQuotaDataDto> quotaResultsAll(ExamQuotaDataDto examQuotaDataDto, HttpServletRequest request, HttpServletResponse response) {
		examQuotaDataDto.setPage(new Page<>(request, response));
		return dcSupExamSumQuotaInfoService.quotaResultsAll(examQuotaDataDto);
	}


	@RequestMapping("quotaDetail")
	public String quotaDetail(Model model,DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo){
		model.addAttribute("dcSupExamSumQuotaInfo", dcSupExamSumQuotaInfo);
		ExamQuotaDataDto dcSupExamSumQuotaInfoQuery = new ExamQuotaDataDto();
		dcSupExamSumQuotaInfoQuery.setExamYear(dcSupExamSumQuotaInfo.getExamYear());

		dcSupExamSumQuotaInfoQuery.setExameOrg(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		dcSupExamSumQuotaInfoQuery.setExamId(dcSupExamSumQuotaInfo.getExamId());
		List<ExamQuotaDataDto> list = dcSupExamSumQuotaInfoService.quotaPlayListDataList(dcSupExamSumQuotaInfoQuery);
		GroupByTypeCount groupByTypeCount= dcSupExamSumQuotaInfoService.groupByQuotaTypeCount(dcSupExamSumQuotaInfoQuery);
		Double count = list.stream().collect(Collectors.summingDouble(ExamQuotaDataDto::getQuotaWeight));
		model.addAttribute("number", count);//指标汇总
		model.addAttribute("groupByTypeCount",groupByTypeCount);
		return "modules/exam/scoresResult/dcSupExamQuotaPlay";
	}

	@RequestMapping("quotaDetail2")
	public String quotaDetail2(String examId, String examYear,Long versionNumber, Model model){
		DcSupExamSumQuota dcSupExamSumQuota = new DcSupExamSumQuota();
		dcSupExamSumQuota.setRemarks2(examYear);
		dcSupExamSumQuota.setExamId(examId);
		dcSupExamSumQuota.setVersionNumber(versionNumber);
		model.addAttribute("dcSupExamSumQuota", dcSupExamSumQuota);
		List<DcSupExamQuotaOrg> list = dcSupExamQuotaOrgService.findOrgByExamIdAndVersion(dcSupExamSumQuota);
		Integer isPush= dcSupExamSumQuotaInfoService.isPush(dcSupExamSumQuota.getExamId(),"2");
		model.addAttribute("assessmentUnit", list);
		model.addAttribute("selectExamOrg", list.get(0).getExameOrg());
		model.addAttribute("isPush",isPush==null?999:isPush);
		return "modules/exam/scoresResult/dcSupExamQuotaPlayLeft2";
	}
	@RequestMapping("viewRight")
	public String subView(@RequestParam("examId") String examId, @RequestParam("exameOrg")String exameOrg, @RequestParam("exameOrgName")String exameOrgName, @RequestParam("examYear")String examYear, @RequestParam("versionNumber")Long versionNumber , Model model){
		DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo = new DcSupExamSumQuotaInfo();
		dcSupExamSumQuotaInfo.setExamId(examId);
		dcSupExamSumQuotaInfo.setExameOrg(exameOrg);
		dcSupExamSumQuotaInfo.setExamYear(examYear);
		dcSupExamSumQuotaInfo.setVersionNumber(versionNumber);
		dcSupExamSumQuotaInfo.setExameOrgName(exameOrgName);
		model.addAttribute("dcSupExamSumQuotaInfo", dcSupExamSumQuotaInfo);
		ExamQuotaDataDto dcSupExamSumQuotaInfoQuery = new ExamQuotaDataDto();
		dcSupExamSumQuotaInfoQuery.setExamYear(examYear);
		dcSupExamSumQuotaInfoQuery.setExameOrg(exameOrg);
		dcSupExamSumQuotaInfoQuery.setExamId(examId);
		List<ExamQuotaDataDto> list = dcSupExamSumQuotaInfoService.quotaPlayListDataList(dcSupExamSumQuotaInfoQuery);
		Double count = list.stream().collect(Collectors.summingDouble(ExamQuotaDataDto::getQuotaWeight));
		model.addAttribute("number", count);//指标汇总
		return "modules/exam/scoresResult/dcSupExamQuotaPlay2";
	}



	@RequestMapping("quotaPlayListData")
	@ResponseBody
	public Page<ExamQuotaDataDto> quotaPlayListData(ExamQuotaDataDto dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response){
		dcSupExamSumQuotaInfo.setPage(new Page<>(request, response));
//		dcSupExamSumQuotaInfo.getPage().setPageSize(999);
		Page<ExamQuotaDataDto> page = dcSupExamSumQuotaInfoService.quotaPlayListData(dcSupExamSumQuotaInfo);
		return page;
	}
	@RequestMapping("quotaPlayListData2")
	@ResponseBody
	public Page<ExamQuotaDataDto> quotaPlayListData2(ExamQuotaDataDto dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response){
		dcSupExamSumQuotaInfo.setPage(new Page<>(request, response));
//		dcSupExamSumQuotaInfo.getPage().setPageSize(999);
		Page<ExamQuotaDataDto> page = dcSupExamSumQuotaInfoService.quotaPlayListData2(dcSupExamSumQuotaInfo);
		return page;
	}


	@GetMapping("/exportQuotaPlayListData2")
	public void exportQuotaPlayListData2(ExamQuotaDataDto dcSupExamSumQuotaInfo,HttpServletResponse response) throws IOException {
		//查询所有用户
		List<ExamQuotaDataExport> list = dcSupExamSumQuotaInfoService.exportQuotaPlayListData2(dcSupExamSumQuotaInfo);

		//在内存操作，写到浏览器
		ExcelWriter writer= ExcelUtil.getWriter(true);
		//自定义标题别名
		writer.addHeaderAlias("exameOrgName","被考核单位");
		writer.addHeaderAlias("quotaType","类别");
		writer.setColumnWidth(0, 15);
		writer.addHeaderAlias("quotaContent","目标内容");
		writer.setColumnWidth(1, 23);
		writer.addHeaderAlias("quotaUnit","单位");


		writer.addHeaderAlias("quotaWeight","权数");
		writer.addHeaderAlias("currentYaerGoal","本年度目标");
		writer.setColumnWidth(5, 23);
		writer.addHeaderAlias("assessMethod","考核方式");
		writer.setColumnWidth(6, 15);

		writer.addHeaderAlias("lastYaerGoal","上一年度目标任务");
		writer.setColumnWidth(7, 23);
		writer.addHeaderAlias("lastYearComplete","上一年度完成情况");
		writer.setColumnWidth(8, 23);
		writer.addHeaderAlias("remarks","备注");
		writer.addHeaderAlias("gradeOrgName","被考核评分单位");
		//默认配置
		writer.write(list,true);
		//设置content—type
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
		//设置标题
		String fileName= URLEncoder.encode(dcSupExamSumQuotaInfo.getExameOrgName()+"-指标","UTF-8");
		//Content-disposition是MIME协议的扩展，MIME协议指示MIME用户代理如何显示附加的文件。
		response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
		ServletOutputStream outputStream= response.getOutputStream();

		//将Writer刷新到OutPut
		writer.flush(outputStream,true);
		outputStream.close();
		writer.close();
	}

	@RequestMapping("selectOrgs")
	@ResponseBody
	public List<Map<String, Object>> selectOrgs(String excludeCode){
		List<Map<String, Object>> mapList = dcSupExamSumQuotaInfoService.selectOrgs(excludeCode);
		return mapList;
	}

	@RequestMapping("chooseOrgs")
	@ResponseBody
	public List<Map<String, Object>> chooseOrgs(String examId){
		List<Map<String, Object>> mapList = dcSupExamSumQuotaInfoService.chooseOrgs(examId);
		return mapList;
	}
	@RequestMapping(value = "batchUpdateQuota")
	public String batchUpdateQuota(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo,Model model){
		model.addAttribute("dcSupExamSumQuotaInfo",dcSupExamSumQuotaInfo);
		return  "modules/exam/quota/dcSupExamSumQuotaBatchUpdate";
	}

	@RequestMapping("getAssessOrganization")
	@ResponseBody
	public List<DcSupExamSumQuotaInfo> getAssessOrganization(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo){
		List<DcSupExamSumQuotaInfo> list=dcSupExamSumQuotaInfoService.getAssessOrganization(dcSupExamSumQuotaInfo);
		return list;
	}



	@RequestMapping("quotaContentDetail")
	public String quotaContentDetail(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, Model model){
		model.addAttribute("dcSupExamSumQuotaInfo",dcSupExamSumQuotaInfo);
		return "modules/exam/quota/dcSupExamSumQuotaDetailList";
	}

	@RequestMapping("quotaPageList")
	@ResponseBody
	public Page<DcSupExamSumQuotaInfo> quotaPageList(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response){
		dcSupExamSumQuotaInfo.setPage(new Page<>(request,response));
		return dcSupExamSumQuotaInfoService.quotaPageList(dcSupExamSumQuotaInfo);
	}

	@RequestMapping("listOrgByQuota")
	@ResponseBody
	public Page<DcSupExamSumQuotaInfo> listOrgByQuota(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, HttpServletRequest request, HttpServletResponse response){
		dcSupExamSumQuotaInfo.setPage(new Page<>(request,response));
		return dcSupExamSumQuotaInfoService.listOrgByQuota(dcSupExamSumQuotaInfo);
	}

	@RequestMapping("changeQuotaContent")
	public String changeQuotaContent(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, Model model){
		String exameOrgs = dcSupExamSumQuotaInfoService.getDanWei(dcSupExamSumQuotaInfo,"code");
		String exameOrgNames = dcSupExamSumQuotaInfoService.getDanWei(dcSupExamSumQuotaInfo,"name");
		DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo1 = dcSupExamSumQuotaInfoService.listOrgByQuotaOne(dcSupExamSumQuotaInfo);
		dcSupExamSumQuotaInfo1.setExameOrgs(exameOrgs);
		dcSupExamSumQuotaInfo1.setExameOrgNames(exameOrgNames);
		dcSupExamSumQuotaInfo1.setGradeOrg(dcSupExamSumQuotaInfo.getExameOrg());
		dcSupExamSumQuotaInfo1.setGradeOrgName(dcSupExamSumQuotaInfo.getExameOrgName());
		dcSupExamSumQuotaInfo1.setRemarks2(dcSupExamSumQuotaInfo1.getQuotaContent());
		dcSupExamSumQuotaInfo1.setVersionNumber(dcSupExamSumQuotaInfo.getVersionNumber());
		dcSupExamSumQuotaInfo1.setExamId(dcSupExamSumQuotaInfo.getExamId());
		model.addAttribute("dcSupExamSumQuotaInfo",dcSupExamSumQuotaInfo1);
		return "modules/exam/quota/dcSupExamSumQuotaContent";
	}

	@RequestMapping("updateQuotaContent")
	@ResponseBody
	public String updateQuotaContent(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		String content = dcSupExamSumQuotaInfoService.updateQuotaContent(dcSupExamSumQuotaInfo);
		if (!content.equals("ok")) {
			return renderResult(Global.FALSE, text(content));
		}
		return renderResult(Global.TRUE, text(content));
	}

	@RequestMapping("delSumInfo")
	@ResponseBody
	public String delSumInfo(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		String content = dcSupExamSumQuotaInfoService.delSumInfo(dcSupExamSumQuotaInfo);
		if (!content.equals("ok")) {
			return renderResult(Global.FALSE, text(content));
		}
		return renderResult(Global.TRUE, text(content));
	}


	@RequestMapping("updateSumOrg")
	@ResponseBody
	public String updateSumOrg( String dcSupExamSumQuotaInfo) {
		DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo1 = JSONObject.parseObject(dcSupExamSumQuotaInfo, DcSupExamSumQuotaInfo.class);
		String content  = dcSupExamSumQuotaInfoService.updateSumOrg(dcSupExamSumQuotaInfo1);
		if (!content.equals("ok")) {
			return renderResult(Global.FALSE, text(content));
		}
		return renderResult(Global.TRUE, text(content));
	}
	/**
	 * 引入报送单位指标
	 */
	@RequestMapping(value = "introQuotaInfo")
	@ResponseBody
	public Object introQuotaInfo(DcSupExamSumQuota dcSupExamSumQuota) {
		String message = dcSupExamSumQuotaInfoService.introQuotaInfo(dcSupExamSumQuota);
		if (StringUtils.isNotBlank(message)){
			return renderResult(Global.FALSE, text("引入最近报送单位指标失败！"));
		}
		return renderResult(Global.TRUE, text("引入最近报送单位指标成功"));
	}

	// ---------------------- 2023-01-17 update ---------------------------

	/**
	 * 右侧考核指标数据列表信息
	 * @param dcSupExamSumQuotaInfo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "rightInfo")
	public String rightInfo(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo, Model model) {
		model.addAttribute("dcSupExamSumQuotaInfo", dcSupExamSumQuotaInfo);
		Integer isPush= dcSupExamSumQuotaInfoService.isPush(dcSupExamSumQuotaInfo.getExamId(),"2");
		model.addAttribute("isPush",isPush==null?999:isPush);
		if (StringUtils.isEmpty(dcSupExamSumQuotaInfo.getRemarks3()) || "0".equals(dcSupExamSumQuotaInfo.getRemarks3())){
			return "modules/exam/quota/quotasumchange/newExamSumQuotaInfoList";
		}else{
			return "modules/exam/quota/quotasumchange/newExamSumQuotaInfoListDemo";
		}
	}

	/**
	 * 查询关键指标版本信息
	 */
	@RequestMapping(value = "newMakeCopy")
	@ResponseBody
	public Object newMakeCopy(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
		dcSupExamSumQuotaInfoService.makeCopy(dcSupExamSumQuotaInfo);
		return renderResult(Global.TRUE, text("生成副本成功！"));
	}

	@RequestMapping(value = "newExport")
	@ResponseBody
	public void newExport(DcSupExamSumQuota dcSupExamSumQuota,HttpServletRequest request, HttpServletResponse response){
		DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo =new DcSupExamSumQuotaInfo();

		dcSupExamSumQuotaInfo.setExamYear(dcSupExamSumQuota.getRemarks2());
		dcSupExamSumQuotaInfo.setExamId(dcSupExamSumQuota.getExamId());
		dcSupExamSumQuotaInfo.setVersionNumber(dcSupExamSumQuota.getVersionNumber());
		dcSupExamSumQuotaInfo.setExportColumn(dcSupExamSumQuota.getExportColumn());
		dcSupExamSumQuotaInfo.setQuotaTypes(dcSupExamSumQuota.getRemarks3());
		dcSupExamSumQuotaInfoService.export(dcSupExamSumQuotaInfo,request,response);
	}


	/**
	 * 导入指标汇总数据
	 */
	@ResponseBody
	@PostMapping(value = "newImportData")
	public String newImportData(MultipartFile file, String updateSupport,DcSupExamSumQuota dcSupExamSumQuota) {
		try {
			DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo =new DcSupExamSumQuotaInfo();

			dcSupExamSumQuotaInfo.setExamYear(dcSupExamSumQuota.getRemarks2());
			dcSupExamSumQuotaInfo.setExamId(dcSupExamSumQuota.getExamId());

			String message = dcSupExamSumQuotaInfoService.importData(file, dcSupExamSumQuotaInfo);
			return renderResult(Global.TRUE, message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, ex.getMessage());
		}
	}
}